package com.google.code.lf.gfm.service.impl;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import org.springframework.stereotype.Service;

import com.google.code.lf.commons.aspect.Loggable;
import com.google.code.lf.commons.exception.NullArgumentException;
import com.google.code.lf.commons.util.FileHelper;
import com.google.code.lf.commons.xml.bind.helper.XmlFileHelper;
import com.google.code.lf.gfm.model.grisbi.GrisbiFile;
import com.google.code.lf.gfm.model.rules.GrisbiAfterFileImportParamFile;
import com.google.code.lf.gfm.service.IGrisbiAfterFileImportParamFileService;
import com.google.code.lf.gfm.service.IGrisbiAfterFileImportParamFileServiceLocal;
import com.google.code.lf.gfm.service.IGrisbiRuleService;

/**
 * Implémentation du service de manipulation des fichiers de paramétrage {@link GrisbiAfterFileImportParamFile} associés à la règle gérée par le service {@link IGrisbiRuleService#afterImportProcess(GrisbiFile, com.google.code.lf.gfm.model.rules.GrisbiAfterFileImportParamFile)}.
 * <br/>Le binding bean/xml utilise <a href="https://jaxb.dev.java.net/">JAXB</a>.
 *
 * @author gael.lorent-fonfrede
 */
@Loggable
@Service(IGrisbiAfterFileImportParamFileService.BEAN_ID)
public class GrisbiAfterFileImportParamFileService implements IGrisbiAfterFileImportParamFileService, IGrisbiAfterFileImportParamFileServiceLocal {
	/** {@link JAXBContext}, la classe est thread safe et peut-être partagée. */
	private static JAXBContext jaxbContext = XmlFileHelper.initializeJaxbContext(GrisbiAfterFileImportParamFile.class);

	/* (non-Javadoc)
	 * @see com.google.code.lf.gfm.service.IGrisbiAfterFileImportParamFileService#readGrisbiAfterFileImportParamFile(java.io.File)
	 */
	public GrisbiAfterFileImportParamFile readGrisbiAfterFileImportParamFile(final File xmlFile) {
		return XmlFileHelper.readFile(jaxbContext, xmlFile, GrisbiAfterFileImportParamFile.class);
	}

	/* (non-Javadoc)
	 * @see com.google.code.lf.gfm.service.IGrisbiAfterFileImportParamFileService#writeGrisbiAfterFileImportParamFile(com.google.code.lf.gfm.model.rules.GrisbiAfterFileImportParamFile, java.io.File)
	 */
	public void writeGrisbiAfterFileImportParamFile(final GrisbiAfterFileImportParamFile grisbiAfterFileImportParamFile, final File xmlFile) {
		// Validation des paramètres
		if (grisbiAfterFileImportParamFile == null) throw new NullArgumentException("grisbiAfterFileImportParamFile");
		FileHelper.checkFileExistsAndIsReadable(xmlFile);

		// Initialisation du marshaller
		final Marshaller marshaller = XmlFileHelper.initializeMarshaller(jaxbContext, true, null, null, null);

		// Ecriture de l'objet model fourni sous forme de fichier xml
		XmlFileHelper.writeFile(marshaller, grisbiAfterFileImportParamFile, xmlFile);
	}

	/* (non-Javadoc)
	 * @see com.google.code.lf.gfm.service.IGrisbiAfterFileImportParamFileServiceLocal#writeGrisbiAfterFileImportParamFileOnSystemOut(com.google.code.lf.gfm.model.rules.GrisbiAfterFileImportParamFile)
	 */
	public void writeGrisbiAfterFileImportParamFileOnSystemOut(final GrisbiAfterFileImportParamFile grisbiAfterFileImportParamFile) {
		// Validation des paramètres
		if (grisbiAfterFileImportParamFile == null) throw new NullArgumentException("grisbiAfterFileImportParamFile");

		// Initialisation du marshaller
		final Marshaller marshaller = XmlFileHelper.initializeMarshaller(jaxbContext, true, null, null, null);

		// Ecriture de l'objet model fourni sous forme de fichier xml
		XmlFileHelper.writeFile(marshaller, grisbiAfterFileImportParamFile, System.out);
	}
}
